package com.videolist.lingling.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.videolist.lingling.ContentFragment;
import com.videolist.lingling.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 电视直播
 * 请至官方网站查看更多设置
 * https://wiki.videolan.org/VLC_command-line_help/
 */
public class RTSPActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView listView;

    private DrawerLayout drawerLayout;

    private TextView textView;
    /*创建一个Drawerlayout和Toolbar联动的开关*/
    private ActionBarDrawerToggle toggle;
    private Toolbar toolbar;
    final List<String> list = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtsp2);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        initView();

        setActionBar();
            /*设置Drawerlayout开关*/
        setDrawerToggle();

    }




    /*设置ActionBar*/
    private void setActionBar() {
        setSupportActionBar(toolbar);
        /*显示Home图标*/
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /*设置Drawerlayout的开关,并且和Home图标联动*/
    private void setDrawerToggle() {
//        toggle = new ActionBarDrawerToggle(RTSPActivity2.this, drawerLayout, toolbar, 0, 0);
//        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0);
        drawerLayout.addDrawerListener(toggle);
        /*同步drawerlayout的状态*/
        toggle.syncState();
    }

    private void initView() {
//        listView = (ListView) findViewById(R.id.v4_listview);
        drawerLayout = (DrawerLayout) findViewById(R.id.v4_drawerlayout);
        textView = (TextView) findViewById(R.id.v4_text);
        initDate();
    }

    private void initDate() {

        list.add("网易");
        list.add("腾讯");
        list.add("新浪");
        list.add("搜狐");
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(list.get(position));
                showDrawerLayout();
            }
        });
        drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开  不设置则不会默认打开
    }

    private void showDrawerLayout() {
        if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.openDrawer(Gravity.LEFT);
        } else {
            drawerLayout.closeDrawer(Gravity.LEFT);
        }
    }

   @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
        //动态插入一个Fragment到FrameLayout当中
        Fragment contentFragment = new ContentFragment();
        Bundle args = new Bundle();
        args.putString("text", list.get(position));
        contentFragment.setArguments(args);

        FragmentManager fm = getFragmentManager();
   /*     fm.beginTransaction().replace(R.id.content_frame, contentFragment)
                .commit();*/

        drawerLayout.closeDrawer(drawerLayout);
    }
}
